var gv = require('../globalVars.js').gv;


//workaround de la mort ( pour _setNewTiles() )
function newTile(id, posRow, posCol) {
    return {
        id: id,
        pos: {
            row: posRow,
            col: posCol
        },
        canPass: 1,
        humidity: gv.humidity,
        fertility: null,
        fertilityCap: null,
        heroId: null
    }
}

function updTile(map, tile, callback) {
    //On localise la tile courrante
    for(var row = 0; row < map.grid.width; row++) {
        for(var col = 0; col < map.grid.height; col++) {
            if(map.tiles[row][col].id == tile.id) {
                //On update les attribs pouvant être modifiés
                map.tiles[row][col].heroId = tile.heroId;
                map.tiles[row][col].canPass = tile.canPass;
                map.tiles[row][col].humidity = tile.humidity;
                map.tiles[row][col].fertility = tile.fertility;

                var curTile = map.tiles[row][col];

                map.markModified('tiles');
                map.save(function(err) {
                    if(err) console.log(err.message);
                    //Si la tile est saved, on la renvoi dans le callback
                    else callback(curTile);
                });
            }
        }
    }
}


//
//function computeTileIdFromRowCol(row, col, grid) {
//    return ( (row * grid.height) + (col + 1) );
//}

exports.newTile = newTile;
exports.updTile = updTile;
